﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.SqlClient;

using hes.models;

namespace hes.data_access
{

    // AssignmentCategoryDAO handles loading AssignmentCategories for a particular student from the database.
    // It can also AddCategories to the database.
    class AssignmentCategoryDAO
    {
        private SqlConnection db_connection;


        public AssignmentCategoryDAO(SqlConnection connection)
        {
            db_connection = connection;
        }


        // Function: AddCategory
        // Adds a category to the database
        // Preconditions: assumes the paraemters are valid
        // Postconditions: Adds a category to the database with <cat> as the item with <itemid>
        public void AddCategory(AssignmentCategory cat, int itemid)
        {
            SqlCommand add_cat = new SqlCommand("INSERT INTO ASMT_CAT(CAT_NAME, CAT_PERCENT, ITEM_ID) Values('" + cat.CategoryName + "'," + 
                cat.CategoryPercent + "," + itemid + ")", db_connection);

            add_cat.ExecuteNonQuery();
        }


        // Function: Find()
        // Finds all the categories of a particular student and returns them in an array list
        // Preconditions: Assumes the item_id exists within the database
        // Postconditions: The database is not changed at all
        public ArrayList Find(int item_id)
        {

            ArrayList results = new ArrayList();


            SqlCommand getCats = new SqlCommand("SELECT * FROM ASMT_CAT WHERE ITEM_ID = " + item_id, db_connection);
            SqlDataReader reader = getCats.ExecuteReader();
            while (reader.Read())
            {
                AssignmentCategory tmp_cat = new AssignmentCategory();
                tmp_cat.CategoryID = Convert.ToInt32(reader["CAT_ID"]);
                tmp_cat.CategoryName = (string)reader["CAT_NAME"];
                tmp_cat.CategoryPercent = Convert.ToInt32(reader["CAT_PERCENT"]);
                results.Add(tmp_cat);
      
            }

            reader.Close();
            return results;

        }

    }
}
